<?php

namespace App\Models;

use App\Traits\ModelTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    use HasFactory, ModelTrait;

    protected $guarded = [];

    public function sku()
    {
        return $this->hasMany(ProductSku::class, 'product_id', 'id');
    }

    public function cate()
    {
        return $this->belongsTo(ProductCate::class, 'cate_id', 'id');
    }

    public function scopeRecommend($query)
    {
        return $query->where('recommend', 1);
    }

    public function scopeFlag($query)
    {
        return $query->where('flag', 1);
    }

    protected static function boot()
    {
        parent::boot();

        // 删除相关联的sku信息
        static::deleting(function ($model) {
            $model->sku()->delete();
        });
    }
}
